#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#define WIDTH 800
#define HEIGHT 600
#define IMG_PATH "assets/image.png"
using namespace std;

int test11();

int main(int argc, char **args) {
    test11();
    return 0;
}

int test11() {
    const int FPS = 60;
    float frameTime = 0;
    u_int prevTime = 0;
    u_int curTime = 0;
    float delta_time = 0;
    float movespeed = 200.0f;
    const u_int8_t *keyState;
    // variable declarations
    SDL_Window *win = NULL;
    SDL_Renderer *renderer = NULL;
    SDL_Texture *img = NULL;
    int textureWidth, textureHeight; // texture width & height

    // Initialize SDL.
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
        return 1;

    SDL_Rect playerReact;
    SDL_Rect postionReact;


    int fw, fh;
    // create the window and renderer
    // note that the renderer is accelerated
    win = SDL_CreateWindow("FPS", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, 0);
    renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);



    // load our image
    img = IMG_LoadTexture(renderer, IMG_PATH);
    SDL_QueryTexture(img, NULL, NULL, &textureWidth, &textureHeight); // get the width and height of the texture

    fw = textureWidth / 3;
    fh = textureHeight / 4;

    playerReact.x = 0, playerReact.y = 2 * fw;
    playerReact.w = fw;
    playerReact.h = fh;
    postionReact.x = postionReact.y = 0;
    postionReact.w = fw;
    postionReact.h = fh;


    SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0xff, 0xff);

    // main loop
    while (1) {


        prevTime = curTime;
        curTime = SDL_GetTicks();
        delta_time = (curTime - prevTime) / 1000.0f;

        // event handling
        SDL_Event e;
        if (SDL_PollEvent(&e)) {
            if (e.type == SDL_QUIT)
                break;

        }
        keyState = SDL_GetKeyboardState(NULL);
        if (keyState[SDL_SCANCODE_RIGHT]) {
            playerReact.y = 2 * fw;
            postionReact.x += movespeed * delta_time;
            if (postionReact.x > WIDTH - fw) {
                postionReact.x = WIDTH - fw;
            }
        }
        else if (keyState[SDL_SCANCODE_LEFT]) {
            playerReact.y = 1 * fw;
            postionReact.x -= movespeed * delta_time;
            if (postionReact.x < 0) {
                postionReact.x = 0;
            }
        }
        else if (keyState[SDL_SCANCODE_DOWN]) {
            playerReact.y = 0;
            postionReact.y += movespeed * delta_time;
            if (postionReact.y > HEIGHT - fh) {
                postionReact.y = HEIGHT - fh;
            }
        }
        else if (keyState[SDL_SCANCODE_UP]) {
            playerReact.y = 3 * fw;
            postionReact.y -= movespeed * delta_time;
            if (postionReact.y < 0) {
                postionReact.y = 0;
            }
        }
        frameTime += delta_time;

        if (frameTime >= 0.25f) {
            frameTime = 0;
            playerReact.x += fw;
            if (playerReact.x >= textureWidth) {
                playerReact.x = 0;
            }
        }

        // clear the screen
        SDL_RenderClear(renderer);
        // copy the texture to the rendering context
        SDL_RenderCopy(renderer, img, &playerReact, &postionReact);
        // flip the backbuffer
        // this means that everything that we prepared behind the screens is actually shown
        SDL_RenderPresent(renderer);

    }

    SDL_DestroyTexture(img);
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(win);

    return 0;
}